package com.qgqaqgqa.deviceinfomanager.adapter;


import android.app.Activity;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.qgqaqgqa.deviceinfomanager.R;
import com.qgqaqgqa.deviceinfomanager.intf.OnRecyclerViewItemClickListener;
import com.qgqaqgqa.deviceinfomanager.model.DeviceInfoModel;
import com.qgqaqgqa.deviceinfomanager.util.utilcode.util.TimeUtils;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目详情
 * User: Created by 钱昱凯
 * Date: 2019/11/14
 * Time: 21:36
 * EMail: 342744291@qq.com
 */
public class FileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_TITLE = 0;
    private static final int VIEW_TYPE_DATA = VIEW_TYPE_TITLE+1;
    private Activity mContext;
    private List<Object> mItems;
    private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;
    private boolean canBack;

    public boolean isCanBack() {
        return canBack;
    }

    public void setCanBack(boolean canBack) {
        this.canBack = canBack;
    }

    public FileAdapter(Activity mContext, List<Object> items) {
        this.mContext = mContext;
        this.mItems = items;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        mOnRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }
    @Override
    public int getItemViewType(int position) {
        if(mItems.get(position) instanceof String) {
            return VIEW_TYPE_TITLE;
        }else if(mItems.get(position) instanceof File) {
            return VIEW_TYPE_DATA;
        }else{
            return VIEW_TYPE_TITLE;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(VIEW_TYPE_TITLE==viewType){
            return new TitleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_file_title, parent, false));
        }else if(VIEW_TYPE_DATA == viewType){
            return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_file, parent, false));
        }
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof TitleViewHolder)
            ((TitleViewHolder) holder).init(position);
        else if(holder instanceof MyViewHolder)
        ((MyViewHolder) holder).init(position);
    }

    @Override
    public int getItemCount() {
        return mItems.size();// == null || mItems.size() == 0 ? 0 : ((mItems.size() - 1) / 5 + 1) * 5;
    }

    class TitleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private int mPosition = 0;


        public TitleViewHolder(View inflate) {
            super(inflate);
            ButterKnife.bind(this, inflate);
            itemView.setOnClickListener(this);
        }

        public void init(int position) {
            this.mPosition = position;
        }

        public void onClick(View v) {
            if (mOnRecyclerViewItemClickListener != null) {
                mOnRecyclerViewItemClickListener.onRecyclerViewItemClick(v, this.mPosition);
            }
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.iv_type)
        ImageView ivType;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_count)
        TextView tvCount;
        @BindView(R.id.tv_time)
        TextView tvTime;

        private int mPosition = 0;
        private File m;

        public MyViewHolder(View inflate) {
            super(inflate);
            ButterKnife.bind(this, inflate);
            itemView.setOnClickListener(this);


        }

        public void init(int position) {
            mPosition = position;
            m=(File)mItems.get(position);
            ivType.setVisibility(m.isDirectory()?View.VISIBLE:View.GONE);
            tvName.setText(m.getName());
            if(m.isDirectory()){
                tvCount.setText(String.format("%d个", m.listFiles().length));
            }else {
                tvCount.setText("");
            }
            tvTime.setText(TimeUtils.millis2String(m.lastModified()));
        }

        @Override
        public void onClick(View v) {
            if (mOnRecyclerViewItemClickListener != null) {
                mOnRecyclerViewItemClickListener.onRecyclerViewItemClick(v, mPosition);
            }
        }
    }
}
